package com.achun.getcouponleague.presenter.impl;

import com.achun.getcouponleague.Bean.Red_Packets;
import com.achun.getcouponleague.model.ServiceApi;
import com.achun.getcouponleague.presenter.IRed_packetPresenter;
import com.achun.getcouponleague.utils.Logutils;
import com.achun.getcouponleague.utils.RetrogitManager;
import com.achun.getcouponleague.view.IRed_packedCallBack;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class Red_packetPresenter implements IRed_packetPresenter {
    private int currentpage=1;
    private IRed_packedCallBack iRed_packedCallBack;
    @Override
    public void registerCallback(IRed_packedCallBack callback) {
            this.iRed_packedCallBack=callback;
    }

    @Override
    public void unRegisterCallback(IRed_packedCallBack callback) {
        if (iRed_packedCallBack != null) {
            iRed_packedCallBack=null;
        }
    }

    @Override
    public void getRed_PacketDatas() {
        if (iRed_packedCallBack != null) {
            iRed_packedCallBack.onLoading();
        }
        Logutils.e(this.getClass(),"特惠--》加载");
        Retrofit retrofit = RetrogitManager.getInstance().getRetrofit();
        ServiceApi serviceApi = retrofit.create(ServiceApi.class);
        Call<Red_Packets> task = serviceApi.getRed_Packets(currentpage);
        task.enqueue(new Callback<Red_Packets>() {
            @Override
            public void onResponse(Call<Red_Packets> call, Response<Red_Packets> response) {
                int code = response.code();
                Logutils.e(this.getClass(),"网络请求码"+code);
                if (code== HttpURLConnection.HTTP_OK){
                    Red_Packets red_packets = response.body();
                    handleOnSuccess(red_packets);
                }else {
                    //网络错误
                    handleOnError();
                }
            }

            @Override
            public void onFailure(Call<Red_Packets> call, Throwable t) {
//网络错误
                Logutils.e(this.getClass(),"网络错误--》"+t.toString());
                handleOnError();

            }
        });

    }

    private void handleOnError() {
        if (iRed_packedCallBack != null) {
            iRed_packedCallBack.onError();
        }
    }

    private void handleOnSuccess(Red_Packets red_packets) {
        currentpage++;
        iRed_packedCallBack.OnLoadedRed_packedDatas(red_packets);
    }

    @Override
    public void getLoadMoreRed_Packet(boolean isrefresh) {
        Retrofit retrofit = RetrogitManager.getInstance().getRetrofit();
        ServiceApi serviceApi = retrofit.create(ServiceApi.class);
        Call<Red_Packets> task = serviceApi.getRed_Packets(currentpage);
        task.enqueue(new Callback<Red_Packets>() {
            @Override
            public void onResponse(Call<Red_Packets> call, Response<Red_Packets> response) {
                int code = response.code();
                Logutils.e(this.getClass(),"网络请求码"+code);
                if (code== HttpURLConnection.HTTP_OK){
                    Red_Packets red_packets = response.body();
                    handleOnMoreSuccess(red_packets,isrefresh);
                }else {
                    //网络错误
                    handleOnMoreError();
                }
            }

            @Override
            public void onFailure(Call<Red_Packets> call, Throwable t) {
//网络错误
                Logutils.e(this.getClass(),"网络错误--》"+t.toString());
                handleOnMoreError();
            }
        });
    }

    private void handleOnMoreError() {
        if (iRed_packedCallBack != null) {
            iRed_packedCallBack.OnloadMoreError();
        }
    }

    private void handleOnMoreSuccess(Red_Packets red_packets,boolean isrefresh) {
        currentpage++;
        if (iRed_packedCallBack != null) {
            iRed_packedCallBack.OnloadMoreRed_packedDatas(red_packets,isrefresh);
        }
    }
}
